package org.dfl.core.model.transitions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dfl.core.data.types.Type;

/**
 * transition with fixed inputs (inputs definitions are created once and cannot be changed)
 * @author Piotr Wlodarczyk
 *
 */
public abstract class FixedInputsTransition extends MultipleInputsTransition{
	@Override
	protected void init() {
		super.init();
		initInputDefinitions();
	}
	protected abstract void initInputDefinitions();
	
	/**
	 * as we add/remove MaxInputs/Min Inputs counter 
	 * while adding/removeing inputs definitions
	 * it's blocked to block possible problems
	 */
	@Override
	final protected int getDefaultMaxInputs() {
		return 0;
	}

	/**
	 * as we add/remove MaxInputs/Min Inputs counter 
	 * while adding/removeing inputs definitions
	 * it's blocked to block possible problems
	 */
	@Override
	final protected int getDefaultMinInputs() {
		return 0;
	}

	public int inputsCount(){
		return getInputsDefinition().size(); 
	}

	@Override
	public void addInputDefinition(String key, Type type) throws Exception{
		maxInputs++;
		minInputs++;
		super.addInputDefinition(key, type);
	}

	/**
	 * pobiera kolekcje wolnych wejsc o danym typie
	 * @param type
	 * @return collection of inputs names or null if any name is available
	 */
	@Override
	public List<String> getFreeInputs(Type type){
		List<String> res = new ArrayList<String>();
		Iterator<String> iter = getInputsDefinition().keySet().iterator();
		while(iter.hasNext()){
			String key = (String) iter.next();
			if(hasFreeInput(key) && type.canMerge((getInputsDefinition().get(key).getType()))){
				res.add(key);
			}
		}
		return res;
	}
	@Override
	public void removeInputDefinition(String key){
		maxInputs--;
		minInputs--;
		super.removeInputDefinition(key);
	}

	@Override
	protected boolean isAutoAddRemoveInputDefinitions() {
		return false;
	}

	//	--------------------------------------------------- OPISY ------------------------------------------------- //
	/**
	 * pobiera opis wejsc
	 * @return
	 */
	protected String getInputDescription(){
		StringBuffer res = new StringBuffer();
		Iterator<String> inputsKeysIter = getInputsDefinition().keySet().iterator();
		while(inputsKeysIter.hasNext()){
			String key = (String) inputsKeysIter.next();
			String value = getInputsDefinition().get(key).getType().toString();
			res.append("	"+key+"="+value+"\n");
		}
		return res.toString();
	}
}
